package com.cluck.alipay.controller;

import com.cluck.alipay.entity.Order;
import com.cluck.alipay.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单控制器
 */
@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 跳转到订单列表
     * @return
     */
    @RequestMapping("/toOrderListPage")
    public ModelAndView toOrderListPage() {
        ModelAndView mav = new ModelAndView();
        mav.addObject("title", "订单列表");
        mav.setViewName("orderList");
        return mav;
    }

    /**
     * 分页查询列表
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("/orderList")
    public Map<String, Object> orderList(@RequestParam("page")Integer page,
                                         @RequestParam("limit")Integer limit) {
       Map<String, Object> map = new HashMap<>();
        Order order = new Order();
        Page<Order> pageList = orderService.list(order, page, limit);
        List<Order> orderList = pageList.getContent();
        long count = pageList.getTotalElements();
        map.put("data", orderList);
        map.put("count", count);
        map.put("code", 0);
        return map;
    }
}
